<template>
  <el-dialog
    v-model="dialogFormVisible"
    :close-on-click-modal="false"
    :title="title"
    width="500px"
    @close="close"
  >
    <el-alert
      :closable="false"
      show-icon
      :title="
        !row ? '初始密码默认为账号' : '重置密码可强制修改为默认密码123456'
      "
      type="warning"
    />
    <el-form ref="formRef" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="账号" prop="username">
        <el-input v-model.trim="form.username" :disabled="!!row" />
      </el-form-item>
      <el-form-item label="角色" prop="groupId">
        <el-select v-model="form.groupId" filterable placeholder="请选择">
          <el-option
            v-for="item in list"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <div style="margin-left: 80px">
        <el-button
          v-show="row"
          style="
            background-color: #fa6567;
            color: #fff;
            border: 0px;
            font-size: 14px;
          "
          @click="resetPassword"
        >
          重置密码
        </el-button>
      </div>
    </el-form>
    <template #footer>
      <el-button style="font-size: 16px" @click="close">取 消</el-button>
      <el-button
        style="
          background-color: #58b5e1;
          color: #fff;
          border: 0px;
          font-size: 14px;
        "
        @click="save"
      >
        确 定
      </el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { defineComponent, getCurrentInstance, reactive, toRefs } from 'vue'
  import { doEdit, toResetPassword } from '@/api/system_admin'
  import { getRoleList } from '@/api/system_admin'

  export default defineComponent({
    name: 'TableEdit',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const { proxy } = getCurrentInstance()
      const state = reactive({
        formRef: null,
        title: null,
        list: [],
        row: null,
        form: {
          username: '',
          groupId: '',
          id: null,
        },
        rules: {
          username: [
            { required: true, trigger: 'blur', message: '请输入用户名' },
          ],
          groupId: [{ required: true, trigger: 'blur', message: '请选择角色' }],
        },
        username: '',
        dialogFormVisible: false,
      })

      const showEdit = (row) => {
        getRoles()
        if (!row) {
          state.title = '添加'
        } else {
          state.title = '编辑'
          // state.form = Object.assign({}, row)
          state.form.username = row.username
          state.form.groupId = row.groupId
          state.form.id = row.id
          state.row = row
        }
        state.dialogFormVisible = true
      }
      const getRoles = async () => {
        const {
          data: { list },
        } = await getRoleList()
        state.list = list
      }
      const close = () => {
        state['formRef'].resetFields()
        state.form = {
          username: '',
          groupId: '',
          id: null,
        }
        state.dialogFormVisible = false
        state.row = null
      }
      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const { msg } = await doEdit(state.form)
            proxy.$baseMessage(msg, 'success', 'vab-hey-message-success')
            emit('fetch-data')
            close()
          }
        })
      }

      const resetPassword = async () => {
        const { msg } = await toResetPassword({ id: state.form.id })
        proxy.$baseMessage(msg, 'success', 'vab-hey-message-success')
      }

      return {
        ...toRefs(state),
        showEdit,
        close,
        save,
        resetPassword,
      }
    },
    mounted() {},
    methods: {},
  })
</script>
